//
//  DetailCollectionView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/16.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "DetailImageView.h"
#import "PDHeaderView.h"

#import <JavaScriptCore/JavaScriptCore.h>
@protocol JSObjcDelegate <JSExport>
//tianbai对象调用的JavaScript方法，必须声明！！！
- (void)callBack:(id)img;

@end

CGFloat margin = 5;
@interface DetailImageView()<UIWebViewDelegate,JSObjcDelegate>{
    
}


@property(nonatomic,strong)UIWebView * detailwebView;

@property(nonatomic,strong)JSContext * jsContext;


@end
@implementation DetailImageView

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
    self.backgroundColor = RandColor;
    [self addSubview:self.detailwebView];
    }
    return self;
}

-(void)setDetailImageArray:(NSArray *)detailImageArray{
    _detailImageArray = detailImageArray;
    JSValue * layouImages = self.jsContext[@"layoutImages"];
    [layouImages callWithArguments:@[_detailImageArray]];
}


-(UIWebView*)detailwebView{
    if(_detailwebView == nil){
        _detailwebView = [[UIWebView alloc] initWithFrame:self.bounds];
        _detailwebView.scrollView.contentInset = (UIEdgeInsets){-64,0,0,0};
        _detailwebView.delegate = self;
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"html_detail" ofType:@"html"];
        NSURL * baseUrl = [NSURL fileURLWithPath:filePath];
        NSString * htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        [_detailwebView loadHTMLString:htmlString baseURL:baseUrl];
    }
    return _detailwebView;
}


-(void)webViewDidFinishLoad:(UIWebView *)webView{
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext = context;
    self.jsContext[@"ios"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
}

-(void)callBack:(id)img{
//    NSLog(@"%@",img);
}

@end
